package com.ag.hmk.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbAidgoodsku;
import com.ag.bean.TbAidgoogs;
import com.ag.bean.TbGoods;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
/**
 * 
 * @author hmk
 * 查询物资列表（分页查询）
 */
public class FindSomeGoodsAction extends BaseAction {

	private static final long serialVersionUID = 23243241L;
	
	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private int maxRow;
	private String gtypeID1;
	private String gName1;
	
	private String gID;  
	private String findType1;
	
	private PageUtil pageUtil; //返回结果
	private List<String> gkuList;
	
	private TbGoods goods;
	private String gkuStr;
	private String ajaxString;
	
	/*
	 * 查询物资列表（分页查询）
	 */
	public String findGoodsList()throws Exception{
		PageUtil pUtil = new PageUtil();
		List result=null;
		List<String> strList = new ArrayList<String>();
		int total=0;
		try {
			String cql = "select count(*) from TbGoods g where 1=1";
			String hql = "from TbGoods g  left join fetch g.tbGoodstype  left join fetch g.tbUnit " +
					"left join fetch g.tbAidgoogses ag left join fetch ag.tbGoodsstatus left join fetch ag.tbAidgoodsku where 1=1";
			System.out.println("----"+gtypeID1);
			if (gtypeID1!=null && gtypeID1.length()>0) {
				cql += " and g.tbGoodstype.gtId = " + gtypeID1;
				hql += " and g.tbGoodstype.gtId = " + gtypeID1;
			}
			System.out.println("----"+gName1);
			if (gName1!=null && gName1.length()>0) {
				cql += " and g.goodsName like '%" + gName1+"%'";
				hql += " and g.goodsName like '%" + gName1+"%'";
			}
			
			cql+=" and g.a='"+IDUtil.PRESERVE+"' ";
			hql+=" and g.a='"+IDUtil.PRESERVE+"' order by g.goodsId desc";
			//查询条数
			total = hmkUiltServImpl.getCount(cql);
			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			//分页查询
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
			if(result!=null){
				Iterator it1 = result.iterator();
				while(it1.hasNext()){
					String str1 = "";
					int i = 0;
					TbGoods g1 = (TbGoods) it1.next(); 
					Set<TbAidgoogs> agSet = g1.getTbAidgoogses();
					Iterator<TbAidgoogs> it2 = agSet.iterator();
					while(it2.hasNext()){
						TbAidgoogs ag2 =(TbAidgoogs)it2.next();
						TbAidgoodsku gku2 = (TbAidgoodsku)ag2.getTbAidgoodsku(); 
						str1 +=(gku2.getAidName()+",");
						i++;
						if (i>4) {
							break;
						}
					}
					str1 +="...";
					strList.add(str1);
				}
			}	
		} catch (Exception e) {
			e.printStackTrace();
			result=null;
			strList = null;
			total=0;
		}
		
		this.setGkuList(strList); //设置物品串
		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		this.setPageUtil(pUtil);
		
		return "getGoodsListOK";
		
	}
	
	/*
	 * 查找一个物资
	 */
	public String findOneGood() throws Exception {
		List<String> strList = new ArrayList<String>();
		List<TbGoods> gList = null;
		try {
			String hql = "from TbGoods g  left join fetch g.tbGoodstype  left join fetch g.tbUnit " +
			"left join fetch g.tbAidgoogses ag left join fetch ag.tbGoodsstatus left join fetch ag.tbAidgoodsku where 1=1";
			
			hql += " and g.goodsId = " + gID;
			
			gList = hmkUiltServImpl.hqlQuery(hql);
			if (gList!=null) {
				Iterator<TbGoods> it1 = gList.iterator();
				if (it1.hasNext()) {
					TbGoods g2 = it1.next();
					String str1 = "";
					Set<TbAidgoogs> agSet = g2.getTbAidgoogses();
					Iterator<TbAidgoogs> it2 = agSet.iterator();
					while(it2.hasNext()){
						TbAidgoogs ag2 =(TbAidgoogs)it2.next();
						TbAidgoodsku gku2 = (TbAidgoodsku)ag2.getTbAidgoodsku(); 
						str1 +=("物资名称："+gku2.getAidName()+"  ，  数量："+ag2.getAgNumber()+g2.getTbUnit().getUnitName()+" ，状态："+ag2.getTbGoodsstatus().getGstatusName()+" ；<br />");
					}
					this.setGkuStr(str1);
					strList.add(str1);
					this.setGoods(g2);
				}
			} else {
				System.out.println("============没找到");
			}
		} catch (Exception e) {
			e.printStackTrace();
			gList = null;
			strList = null;
		}
		
		if (findType1!=null && findType1.equals("1")) {
			//返回到物资编辑
			return "gotoGEditOK";
		} else  if(findType1!=null && findType1.equals("2")){
			//返回到事件关联物资详情
			return "gotoeventWuzhiDetail";
		}else{
			//返回到物资详情
			return "getOneGoodOK";
		}
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public List<String> getGkuList() {
		return gkuList;
	}

	public void setGkuList(List<String> gkuList) {
		this.gkuList = gkuList;
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setGtypeID1(String gtypeID1) {
		this.gtypeID1 = gtypeID1;
	}

	public void setGName1(String name1) {
		gName1 = name1;
	}

	public TbGoods getGoods() {
		return goods;
	}

	public void setGoods(TbGoods goods) {
		this.goods = goods;
	}

	public String getGkuStr() {
		return gkuStr;
	}

	public void setGkuStr(String gkuStr) {
		this.gkuStr = gkuStr;
	}

	public void setGID(String gid) {
		gID = gid;
	}

	public String getAjaxString() {
		return ajaxString;
	}

	public void setAjaxString(String ajaxString) {
		this.ajaxString = ajaxString;
	}

	public void setFindType1(String findType1) {
		this.findType1 = findType1;
	}
}
